﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class TemplateHierarchy : BaseItem
    {
        private TemplateHierarchy() : base() { }
        private TemplateHierarchy(Hashtable table) : base(table) { }
        private TemplateHierarchy(Hashtable table, List<TemplateHierarchy> collection) : base(table, collection) { }

        public static TemplateHierarchy CreateInstance()
        {
            TemplateHierarchy tmp = new TemplateHierarchy();
            tmp.Sequence = -1;
            return tmp;
        }

        private static TemplateHierarchy Get(Hashtable table)
        {
            if (table != null)
            {
                return new TemplateHierarchy(table);
            }
            else
            {
                return null;
            }
        }

        private static List<TemplateHierarchy> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<TemplateHierarchy> tmp = new List<TemplateHierarchy>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new TemplateHierarchy(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        protected override Base CreateDal()
        {
            return new TemplateHierarchyDal();
        }

        protected override void Load(Hashtable table)
        {
            Id = (int)table[TemplateHierarchyDal.Fields.Id];
            ParentId = (int)table[TemplateHierarchyDal.Fields.ParentId];
            ParentName = (string)table[TemplateHierarchyDal.Fields.ParentName];
            ChildId = (int)table[TemplateHierarchyDal.Fields.ChildId];
            ChildName = (string)table[TemplateHierarchyDal.Fields.ChildName];
            Sequence = (int)table[TemplateHierarchyDal.Fields.Sequence];
        }

        public int Id { get; private set; }
        public int ParentId { get; set; }
        public string ParentName { get; private set; }
        public int ChildId { get; set; }
        public string ChildName { get; private set; }
        public int Sequence { get; set; }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int idOut = Id;
            string parentNameOut = ParentName;
            string childNameOut = ChildName;
            int sequenceOut = Sequence;
            ((TemplateHierarchyDal)dal).Update(ref idOut, ParentId, ref parentNameOut, ChildId, ref childNameOut, ref sequenceOut, ref concurencyId);
            Id = idOut;
            ParentName = parentNameOut;
            ChildName = childNameOut;
            Sequence = sequenceOut;
            return Id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateHierarchyDal)dal).Delete(Id, concurencyId);
        }

        public static TemplateHierarchy GetById(int id)
        {
            TemplateHierarchyDal dal = new TemplateHierarchyDal();
            return Get(dal.GetById(id));
        }

        public static List<TemplateHierarchy> GetAllByChildId(int childId)
        {
            TemplateHierarchyDal dal = new TemplateHierarchyDal();
            return Get(dal.GetAllByChildId(childId));
        }

        public static List<TemplateHierarchy> GetAllByParentId(int parentId)
        {
            TemplateHierarchyDal dal = new TemplateHierarchyDal();
            return Get(dal.GetAllByParentId(parentId));
        }



        public static void Move(List<TemplateHierarchy> collection, int movingIndex, int replacingIndex)
        {
            //get the moving
            TemplateHierarchy moving = collection[movingIndex];
            //get the move to
            TemplateHierarchy replacing = collection[replacingIndex];

            bool saveAll = false;
            if (moving.Sequence == replacing.Sequence)
            {
                saveAll = true;
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i].Sequence != i)
                    {
                        collection[i].Sequence = i;
                    }
                }
            }

            int tmpSequence = moving.Sequence;
            moving.Sequence = replacing.Sequence;
            replacing.Sequence = tmpSequence;

            if (saveAll)
            {
                collection[0].BeginTransaction();
                foreach (TemplateHierarchy t in collection)
                {
                    if (t != collection[0])
                    {
                        t.UseTransaction(collection[0].TransactionKey);
                    }
                    t.Update();
                }
                collection[0].CommitTransaction();
            }
            else
            {
                moving.BeginTransaction();
                replacing.UseTransaction(moving.TransactionKey);
                moving.Update();
                replacing.Update();
                moving.CommitTransaction();
            }

        }
    }
}
